define(
    ["PDUtilDir/dataService", "PDUtilDir/grid", "PDUtilDir/searchBlock", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect", "Date", "DateCN", "css!DateCss"],
    function (DataService, Grid, SearchBlock, Util, Tool, Slidebar, inputSelect) {
        function addVisitorSupportInit($scope, $compile) {
            return new addVisitorSupport($scope, $compile);
        };

        function addVisitorSupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;
        };
        addVisitorSupport.fn = addVisitorSupport.prototype;

        addVisitorSupport.fn.init = function (multi) {
            var _this = this;

            _this.cache = {
                multi: multi ? false : true
            }

            _this.entity = {
                visitors: new Array()
            };
            _this.search = {};
        };

        addVisitorSupport.fn.digest = function () {
            this.$scope.$digest();
        };

        addVisitorSupport.fn.run = function (multi) {
            var _this = this;
            _this.init(multi);

            this.slidebars = Slidebar({
                size: 'lg',
                url: getServer()
                + "/static/app/order/orderForVisitorProductInfos/views/visitor.html",
                $scope: _this.$scope,
                $compile: _this.$compile,
                afterLoad: function ($Panel) {
                    _this.initialization();
                }
            });
        };
        addVisitorSupport.fn.initialization = function () {
            this.baseInit();
        };
        /* 查询 start */
        addVisitorSupport.fn.searchSupport = function () {
            var _this = this;
            return {
                run: function(){
                    _this.grid.reload({
                        queryParam : {
                            model: JSON.stringify(_this.search)
                        }
                    })
                }
            }
        };
        addVisitorSupport.fn.save = function() {
            DataService.setObject('product.agency.search.visitors', this.entity.visitors);
            this.$scope.agencySupport.reloadVisitorsSupport().run();
            this.slidebars.close();
        };
        addVisitorSupport.fn.baseInit = function () {
            var _this = this;

            console.info(_this.cache.multi);
            // SearchBlock.init('CarSearchBlockId');
            _this.grid = Grid({
                id : "VisitorForSearchGrid",
                multi : _this.cache.multi,
                pagination : true,
                index : true,
                cache : false,
                onSeletedChanged : function(data) {
                    _this.entity.visitors = data;
                    _this.digest();
                },
                layout : [ {
                    name : "中文名",
                    field : "zhName",
                    width: "60px",
                    sort : true
                }, {
                    name : "英文名",
                    field : "enName",
                    width: "60px",
                    sort : true
                }, {
                    name : "性别",
                    field : "sex",
                    width: "40px",
                    format : function(e) {
                        if (e.row.sex == "1" || e.row.sex == 1)
                            return '男';
                        else
                            return '女';
                    }
                }, {
                    name : "年龄",
                    width: "40px",
                    field : "age"
                }, {
                    name : "联系电话",
                    width: "80px",
                    field : "phone"
                }, {
                    name : "证件类型",
                    field : "certificates",
                    width: "60px",
                    format:function(e){
                        return _this.$scope.agencySupport.cache.certificatesObject.get(e.row.certificates);
                    }
                }, {
                    name : "证件号码",
                    field : "certificatesNumber"
                } ],
                toolbar : [ {
                    name : "查询",
                    icon : "fa fa-search",
                    callback : function(event) {
                        _this.searchFun();
                    }
                } ],
                data : {
                    type : 'URL',
                    value : getServer() + '/flea/hclv/visitor/selectByDept.htmls'
                }/*,
                onLoadDataSuccess:function(data){
                    var selecteds = _this.$scope.cache.visitor,
                        len = selecteds.length,
                        i = 0;

                    if(len == 0)
                        return data;

                    for(var n = 0; n < data.length; n++){
                        for(i = 0; i < len; i++){
                            if (data[n] && data[n].id == selecteds[i].id){
                                data.splice(n, 1);
                            }
                        }
                    }
                    return data;
                }*/
            });
        };

        return addVisitorSupportInit;
    });